<template>
  <div class="container">
    <div class="list-container">
      <van-pull-refresh v-model="refreshing" v-model:loading="loading" @refresh="onRefresh">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getList"
        >
          <van-cell v-for="item in list" :key="item.id" :title="item.rq" arrow-direction="right"
                    :value="item.nickName" />
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script setup>

import { onMounted, reactive, ref, toRaw } from 'vue'
import { byNormalApi, byPostponeApi, byUncompletedApi } from '@/api/workLog.js'
import { useRoute } from 'vue-router'

const route = useRoute()
const queryParams = reactive({
  startTime: route.query.startTime,
  endTime: route.query.endTime,
  deptId: route.query.deptId,
  pageSize:20,
  pageNum: 1
})
const list = ref([])
const loading = ref(false)
const finished = ref(false)
const refreshing = ref(false)

const getList = () => {
  let params = toRaw(queryParams)
  if (route.query.type === '1') {
    byNormalApi (toRaw(params)).then(res => {
      if (refreshing.value) {
        list.value = []
        refreshing.value = false
      }
      list.value = [...list.value, ...res.rows]
      loading.value = false
      if (res.rows.length < params.pageSize) {
        finished.value = true
      } else {
        queryParams.pageNum = queryParams.pageNum + 1
      }
    })
  } else if (route.query.type === '2') {
    byPostponeApi(toRaw(params)).then(res => {
      if (refreshing.value) {
        list.value = []
        refreshing.value = false
      }
      list.value = [...list.value, ...res.rows]
      loading.value = false
      if (res.rows.length < params.pageSize) {
        finished.value = true
      } else {
        queryParams.pageNum = queryParams.pageNum + 1
      }
    })
  } else if (route.query.type === '3') {
    byUncompletedApi(toRaw(params)).then(res => {
      if (refreshing.value) {
        list.value = []
        refreshing.value = false
      }
      list.value = [...list.value, ...res.rows]
      loading.value = false
      if (res.rows.length < params.pageSize) {
        finished.value = true
      } else {
        queryParams.pageNum = queryParams.pageNum + 1
      }
    })
  }
}

const onRefresh = () => {
  // 清空列表数据
  finished.value = false
  // 将 loading 设置为 true，表示处于加载状态
  loading.value = true
  getList()
}
const init = () => {
  // getList()
}

onMounted(() => {
  init()
})
</script>

<style scoped lang="less">
.container {
  width: 100vw;
  height: 100%;
  overflow-y: auto;
}

</style>